package com.fang.networkdisk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fang.common.common.CommonPage;
import com.fang.common.exception.ExceptionCode;
import com.fang.common.exception.NetworkDiskException;
import com.fang.networkdisk.entity.bo.PermissionBo;
import com.fang.networkdisk.entity.po.TbNetDisPermission;
import com.fang.networkdisk.entity.vo.PermissionVo;
import com.fang.networkdisk.mapper.TbNetDisPermissionMapper;
import com.fang.networkdisk.service.TbNetDisPermissionService;
import com.fang.networkdisk.util.NDBeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 权限表 服务实现类
 * </p>
 *
 * @author Bernie_fang
 * @since 2024-12-07
 */
@Service
public class TbNetDisPermissionServiceImpl extends ServiceImpl<TbNetDisPermissionMapper, TbNetDisPermission> implements TbNetDisPermissionService {

    @Resource
    private TbNetDisPermissionMapper tbNetDisPermissionMapper;

    @Override
    public CommonPage<List<PermissionVo>> queryPermission(PermissionBo permissionBo) {
        permissionBo.setOffset((permissionBo.getCurPage() - 1) * permissionBo.getPageSize());
        List<PermissionVo> permissionVos = this.tbNetDisPermissionMapper.queryPermission(permissionBo);
        int total = this.tbNetDisPermissionMapper.queryPermissionCount(permissionBo);
        CommonPage result = new CommonPage(permissionBo.getCurPage(), total, permissionVos);
        return result;
    }


    @Override
    public TbNetDisPermission getPermissionsByCode(String code) {
        QueryWrapper<TbNetDisPermission> wrapper = new QueryWrapper<>();
        wrapper.eq("code", code);
        return tbNetDisPermissionMapper.selectOne(wrapper);
    }

    @Override
    public int updatePermissionsById(PermissionBo permissionBo) {
        TbNetDisPermission permission = new TbNetDisPermission();
        NDBeanUtils.copyListProperties(permissionBo, permission);
        return tbNetDisPermissionMapper.updateById(permission);
    }

    @Override
    public int deletePermissionsById(Integer id, String authorId) {
        return tbNetDisPermissionMapper.deletePermissionsById(id, authorId, new Date());
    }

    @Override
    public int addPermissions(PermissionBo permissionBo) {
        TbNetDisPermission db_permissions = this.getPermissionsByCode(permissionBo.getCode());
        if (NDBeanUtils.isObjectNotEmpty(db_permissions)) {
            throw new NetworkDiskException(ExceptionCode.DATA_DUPLICATE);
        }
        TbNetDisPermission permission = new TbNetDisPermission();
        NDBeanUtils.copyListProperties(permissionBo, permission);
        return tbNetDisPermissionMapper.insert(permission);
    }
}
